//
//  ThreeViewController.m
//  项目二
//
//  Created by rimi on 16/5/13.
//  Copyright © 2016年 objective. All rights reserved.
//

#import "ThreeViewController.h"
#import "UIColor+RandomColor.h"
#import "CustomCollectionViewCell.h"
#import "PhotoViewController.h"
@interface ThreeViewController ()<UICollectionViewDelegate,UICollectionViewDataSource>

/**根据布局初始化 collectionview*/
@property(nonatomic,strong)UICollectionView *collectionView;
/**数据源*/
@property(nonatomic,strong)NSMutableArray *array;
/**配置 collectionview 的布局*/
@property(nonatomic,strong)UICollectionViewFlowLayout *layout;
@end

@implementation ThreeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"CollectionView";
    /**初始化数据源*/
    _array = [NSMutableArray array];
    for (NSInteger index = 0; index < 10; index ++) {
       
        [_array addObject:[UIColor specialRandomColor]];
    }
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"照片" style:UIBarButtonItemStylePlain target:self action:@selector(photoButtonTaped:)];
    
    /**配置 collectionview 的布局*/
    UICollectionViewFlowLayout  *layout = [[UICollectionViewFlowLayout alloc] init];
    /**滚动方向*/
    layout.scrollDirection = UICollectionViewScrollDirectionVertical;
    /**设置 item 的尺寸*/
    layout.itemSize = CGSizeMake(100, 100);
    /**item 之间的最小间距*/
    layout.minimumInteritemSpacing = 3;
    /**item 每一行之间的最小间距*/
    layout.minimumLineSpacing = 10;
    /**设置header的尺寸*/
    layout.headerReferenceSize = CGSizeMake(100, 60);
    /**设置footer的尺寸*/
    layout.footerReferenceSize = CGSizeMake(200, 60);
    
    /**根据布局，初始化 collectionview*/
    UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:layout];
    collectionView.backgroundColor = [UIColor whiteColor];
    /**设置内偏移量*/
    collectionView.contentInset = UIEdgeInsetsMake(0, 10, 0, 10);
    /**注册cell*/
    [collectionView registerClass:[CustomCollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
    /**注册header*/
    [collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header"];
    /**注册footer*/
    [collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"footer"];
    /**数据源协议*/
    collectionView.dataSource = self;
    collectionView.delegate = self;
    [self.view addSubview:collectionView];
}
#pragma mark - UIButton
-(void)photoButtonTaped:(UIButton *)sender{
    PhotoViewController *photoVC = [[PhotoViewController alloc]init];
    [self.navigationController pushViewController:photoVC animated:YES];
}

#pragma mark -- uicollection view datasource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return _array.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    CustomCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
        cell.backgroundColor = _array[indexPath.row];
    
    return cell;
}

   /**自定义 header footer*/
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
    if (kind == UICollectionElementKindSectionHeader) {
        //header
        UICollectionReusableView *header = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header" forIndexPath:indexPath];
        
        header.backgroundColor = [UIColor redColor];
        return header;
        
    }
    if (kind == UICollectionElementKindSectionFooter) {
        //footer
        UICollectionReusableView *footer = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"footer" forIndexPath:indexPath];
        
        footer.backgroundColor = [UIColor redColor];
        return footer;
        
    }
    return nil;
}

  /**选中 cell*/
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    
    NSLog(@"%ld",indexPath.row);
    
    /**1.删除item*/
     [_array removeObjectAtIndex:indexPath.row];
     [collectionView deleteItemsAtIndexPaths:@[indexPath]];
    
    /**2.插入 item*/
//     [_array insertObject:[UIColor blackColor] atIndex:indexPath.row];
//     [collectionView insertItemsAtIndexPaths:@[indexPath]];
    
   /**3.移动*/
//    [_array exchangeObjectAtIndex:0 withObjectAtIndex:indexPath.row];
//    NSIndexPath *toIndexPath = [NSIndexPath indexPathForRow:0 inSection:0];
//    [collectionView moveItemAtIndexPath:indexPath toIndexPath:toIndexPath];
}





@end
